package com.xinqi.modules.course.clazz.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditApplyByPhoneDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditCreateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditQueryDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditUpdateDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassJoinAuditResultDTO;
import com.xinqi.modules.course.clazz.service.ClassJoinAuditService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生加入班级审核控制层
 *
 * @author: system
 * @date: 2023/01/17
 */
@Tag(name = "Web-班级审核", description = "班级审核")
@RestController
@RequestMapping("/web/class-join-audit")
@RequiredArgsConstructor
public class ClassJoinAuditWebController {
    private final ClassJoinAuditService classJoinAuditService;

    /**
     * 申请加课分页列表
     *
     * @param pageable 分页策略
     * @param dto      查询参数
     * @return R<PageInfo < ClassJoinAuditResultDTO>>
     */
    @Operation(summary = "学生加入班级审核-分页")
    @GetMapping("/page")
    public R<PageInfo<ClassJoinAuditResultDTO>> page(Param pageable, ClassJoinAuditQueryDTO dto) {
        PageInfo<ClassJoinAuditResultDTO> page = classJoinAuditService.page(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "学生加入班级审核-数量")
    @GetMapping("/count")
    public R<Integer> count(ClassJoinAuditQueryDTO dto) {
        Integer count = classJoinAuditService.count(dto);
        return R.data(count);
    }

    @Operation(summary = "学生加入班级审核-待审核数量-与班级审核方式无关")
    @GetMapping("/{classId}/not-audit-count")
    public R<Long> findNotAuditCountByClassId(@PathVariable("classId") Long classId) {
        Long count = classJoinAuditService.findNotAuditCountByClassId(classId);
        return R.data(count);
    }

    @Operation(summary = "学生加入班级审核-详情")
    @GetMapping("/{auditId}")
    public R<ClassJoinAuditResultDTO> findById(@PathVariable("auditId") Long id) {
        ClassJoinAuditResultDTO dto = classJoinAuditService.detail(id);
        return R.data(dto);
    }

    @Operation(summary = "新增学生加入班级审核")
    @PostMapping
    public R<Long> insert(@Validated @RequestBody ClassJoinAuditCreateDTO dto) {
        return R.data(classJoinAuditService.insert(dto));
    }

    @Operation(summary = "修改学生加入班级审核")
    @PutMapping("/{auditId}")
    public R<Boolean> update(@PathVariable("auditId") Long id, @Validated @RequestBody ClassJoinAuditUpdateDTO dto) {
        dto.setId(id);
        return R.data(classJoinAuditService.updateById(dto));
    }

    @Operation(summary = "删除学生加入班级审核")
    @DeleteMapping("/{auditId}")
    public R<Boolean> delete(@PathVariable("auditId") Long id) {
        return R.data(classJoinAuditService.deleteById(id));
    }

    /**
     * 学生通过扫码/输入加课码的方式申请加课
     *
     * @param dto 申请加课实体
     * @return R<Boolean>
     */
    @Operation(summary = "学生申请加入课程")
    @PostMapping("/apply")
    public R<ClassJoinAuditResultDTO> apply(@Validated @RequestBody ClassJoinAuditCreateDTO dto) {
        return R.data(classJoinAuditService.apply(dto));
    }

    /**
     * 未登录情况下-使用手机号和验证码加课
     */
    @Operation(summary = "使用手机号和验证码加课")
    @PostMapping("/apply-by-phone")
    public R<ClassJoinAuditResultDTO> applyByPhone(@Validated @RequestBody ClassJoinAuditApplyByPhoneDTO dto) {
        return R.data(classJoinAuditService.applyByPhone(dto));
    }

    @Operation(summary = "审核通过")
    @PutMapping("/{auditId}/accept")
    public R<Boolean> accept(@PathVariable("auditId") Long id) {
        return R.data(classJoinAuditService.accept(id));
    }

    @Operation(summary = "审核拒绝")
    @PutMapping("/{auditId}/reject")
    public R<Boolean> reject(@PathVariable("auditId") Long id) {
        return R.data(classJoinAuditService.reject(id));
    }

    @Operation(summary = "批量审核通过")
    @PutMapping("/batch-accept")
    public R<Boolean> batchAccept(@RequestBody List<ClassJoinAuditUpdateDTO> dto) {
        return R.data(classJoinAuditService.batchAccept(dto));
    }

    @Operation(summary = "批量拒绝")
    @PutMapping("/batch-reject")
    public R<Boolean> batchReject(@RequestBody List<ClassJoinAuditUpdateDTO> dto) {
        return R.data(classJoinAuditService.batchReject(dto));
    }
}
